# -*- conding:utf-8 -*-
import config
import aliyun_api.all_dns as all_dns
import aliyun_api.all_domain as all_domain
import public.dingding as dingding
import public.https as https

#获取账户的域名列表
def get_all_domain(key,secret):
    domain_list = []
    for i in all_domain.get_doamin(key,secret)['Data']['Domain']:
        domain_list.append(i['DomainName'])
    return domain_list 

#获取所有域名解析
def get_all_dns(key,secret,domain):
    dns_list = []
    for i in all_dns.get_dns(key,secret,domain)['DomainRecords']['Record']:
        dns_list.append(i['RR'])
    return(dns_list)

#返回有效的https
def get_ssl_status(subdomain):
    return https.check_https(subdomain)


if __name__ == '__main__':
    send_content = 'https证书监测：'
    send_ssl = []
    #循环阿里云账户
    for i in config.ali_auth:
        #获取所有阿里云账户的域名
        for d in get_all_domain(i['key'],i['secret']):
            for s in get_all_dns(i['key'],i['secret'],d):
                subdoamin = str(s) + '.' + str(d)
                ssl_info = get_ssl_status(subdoamin)
                ssl_info['subdomain'] = subdoamin
                print(ssl_info)
                if ssl_info['status'] == 'valid':
                    if ssl_info['remaining_days'] <= 30:
                        send_ssl.append(ssl_info)

    for content in send_ssl:
        send_content = send_content + '\n' + '域名:' + str(content['subdomain']) + '有效期:' + str(content['remaining_days'])

    print(send_content)
    dingding.run(send_content)
